package cn.edu.wfit.modules.directionmanagement.controller;

import cn.edu.wfit.modules.directionmanagement.util.RequestAskUtil;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;

/**
 * 通义千问AI控制器
 * 提供与通义千问3模型的交互功能，支持流式输出
 */
@CrossOrigin
@RestController
@RequestMapping("/ai/qwen")
public class QwenAIController {
    @Autowired
    private RequestAskUtil requestAskUtil;

    @Value("${dashscope.api-key}")
    private String apiKey;

    /**
     * 与通义千问模型进行对话（流式输出）
     * @param message 用户输入的消息
     * @return SSE流式响应
     */
    @RequestMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public ResponseBodyEmitter chatStream(@RequestParam(value = "message") String message) throws NoApiKeyException, InputRequiredException, ApiException, IOException {
        return requestAskUtil.ask(message);
    }
}